<?php

/**
 * Classe Block
 *
 * @version $Id$
 * @copyright 2009
 */
/**
 *
 *
 */
class Block {
	private $id;
	public $name;
	public $filename;
	public $active;
	private $weight;
	/**
	 * Constructor
	 */
	function __construct($id,$name,$filename,$active,$weight){
		$this->id = $id;
		$this->name = $name;
		$this->filename = $filename;
		$this->active = $active;
		$this->weight = $weight;
	}

	public static function get($param){
		if($param == "all") { //$param = "all" si on veut tous les blocks
			$query = "select * from blocks order by weight ASC";
		}
		elseif(is_numerical($param)) { //$param = int si on en veut qu'un
			$query = "select * from blocks where id = ".$param.";";
		}
		$results = DBselect($query);
		$blocks = array();
		foreach ($results as $res){
			$blocks[$res["id"]] = new block($res["id"],$res["name"],$res["filename"],$res["active"],$res["weight"]);
		}
		return $blocks;
	}

	public static function getForCache() {
		$query = "select filename from blocks where active = true order by weight ASC";
		$results = DBselect($query);
		$blocks = array();
		foreach($results as $res) {
			$blocks[] = $res["filename"];
		}
		return $blocks;
	}

	public function setActive() {
		$query = "update blocks set active = true where id = ".$this->id.";";
		$result = DBexec($query);
		return $result;
	}

	public function setInactive() {
		$query = "update blocks set active = false where id = ".$this->id.";";
		$result = DBexec($query);
		return $result;
	}
}

?>